
<title></title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>系统管理</cite></a>
        <a><cite>app版本</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <table class="layui-hide" id="version-list" lay-filter="version-list"></table>
                    <script type="text/html" id="table-tool">
                        <div class="layui-btn-container">
                            <button class="layui-btn layui-btn-sm" lay-event="add">添加新版本</button>
                        </div>
                    </script>
                    <script type="text/html" id="table-oper">
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['admin', 'table','upload'], function(){
        var admin = layui.admin
            ,view = layui.view
            ,form = layui.form
            ,table = layui.table
            ,upload = layui.upload
            ,$ = layui.$

        table.render({
            elem: '#version-list'
            ,url:'/api/admin/system/version'
            ,toolbar: '#table-tool'
            ,cols: [[
                {field:'id', title:'ID', width:80,sort: true}
                ,{field:'version', title:'版本号'}
                ,{field:'role', title:'客户端'}
                ,{field:'url', title:'下载地址'}
                ,{field: 'created_at', title:'创建时间', sort: true}
                ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-oper'}
            ]]
            ,page: true
            ,limit:10
            ,limits:[10,20,50,100]
        });

        const table_form = function(d){
            let data = d || {};

            var load = 0;

            admin.popup({
                title: '添加新版本'
                ,area: ['600px', '450px']
                ,id: 'system-version-edit'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render('system/form/version', data).done(function(){
                        form.render(null, 'layuiadmin-app-form-list');

                        upload.render({
                            elem: '#form-upload' //绑定元素
                            //,exts: 'apk'
                            ,headers:{Authorization:layui.data('shiys').Authorization}
                            ,before:function (){
                                load = layer.load(1);
                            }
                            ,done: function(res){
                                layer.close(load);

                                if (res.code == 0){
                                    $('#form-file').val(res.data.src);
                                    $('#form-url').val(window.location.protocol+"//"+window.location.host+res.data.src);
                                }
                            }
                            ,error: function(){
                                //请求异常回调
                            }
                        });
                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'/api/admin/system/version/post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('version-list'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });

                        });
                    });
                }
            });
        }

        //头工具栏事件
        table.on('toolbar(version-list)', function(obj){

            switch(obj.event){
                case 'add':
                    table_form();
                    break;
            };
        });

        //表格按钮事件
        table.on('tool(version-list)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除此版本？', function(index){

                    admin.req({
                        url: '/api/admin/system/version/delete'
                        ,data:data
                        ,done:function (res) {
                            obj.del();
                            layer.close(index);
                        }
                    });


                });
            }
        });

    });
</script>
